package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.web.exception.CustomException;
import com.etoak.entity.Dishes;
import com.etoak.entity.OrderItem;
import com.etoak.entity.vo.DishesVO;
import com.etoak.mapper.DishesMapper;
import com.etoak.service.DishesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author et2406
 * @since 2024-11-18
 */
@Service
public class DishesServiceImpl extends ServiceImpl<DishesMapper, Dishes> implements DishesService {

    /**
     * 菜品添加操作
     *
     * @param dishes 菜品参数
     */
    @Override
    public void addDishes(Dishes dishes) {
        if (ObjectUtils.isNotEmpty(getByName(dishes.getName()))) {
            throw new CustomException("菜品名称重复!");
        }

        dishes.setCreateTime(DateUtil.now());
        this.save(dishes);
    }

    @Override
    public PageVO<DishesVO> getList(int pageNum, int pageSize, Dishes dishes) {
        PageHelper.startPage(pageNum, pageSize);
        // 在 ThreadLocal 存放一个线程变量
        List<DishesVO> list = baseMapper.getList(dishes);
        PageInfo<DishesVO> pageInfo = new PageInfo<>(list);
        return new PageVO<>(pageNum, pageSize, list, pageInfo.getTotal());
    }


    @Override
    public void update(int id, Dishes dishes) {

        if (ObjectUtils.isEmpty(this.getById(id))) {
            throw new CustomException("菜品不存在");
        }

        /* 菜品名称不能修改为其他菜品的名称 */
        String dishesName = dishes.getName();
        if (StringUtils.isNotEmpty(dishesName)) {
            Dishes savedDishes = this.getByName(dishesName);
            if (ObjectUtils.isNotEmpty(savedDishes) && !savedDishes.getId().equals(id)) {
                throw new CustomException("菜品名单重复");
            }
        }

        /*更新*/
        dishes.setId(id);
        dishes.setUpdateTime(DateUtil.now());
        updateById(dishes);
    }



    @Override
    public void delete(int id) {
        if (ObjectUtils.isEmpty(getById(id))) {
            throw new CustomException("菜品不存在!!");
        }

        this.removeById(id);
    }

    @Override
    public boolean ifUsedByDishes(Dishes dishes) {
        Integer id = dishes.getId();
        return this.lambdaQuery().eq(Dishes::getMain,id)
                .or()
                .eq(Dishes::getMinor,id)
                .or()
                .eq(Dishes::getSeasoning,id)
                .count() > 0;
    }

    @Override
    public List<DishesVO> getDishesByDishesId(List<OrderItem> orderItemList) {
        List<DishesVO> dishesVOS = new ArrayList<>();

        // jdk1.8之后添加的增强forEach
        orderItemList.forEach( orderItem -> {
            Integer dishesId = orderItem.getDishesId();
            DishesVO disheByDisheId = baseMapper.getDisheByDisheId(dishesId);
            disheByDisheId.setCount(orderItem.getDishesNum());

            dishesVOS.add(disheByDisheId);
        } );

        return dishesVOS;
    }

    /**
     * 查询是否重复
     *
     * @param name 菜品名
     * @return
     */
    private Dishes getByName(String name) {
        return lambdaQuery().eq(Dishes::getName, name).one();
    }
}
